package com.zhejiangzhengyuan.municipal_manhole_cover.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.Device;
import com.zhejiangzhengyuan.municipal_common.entity.DeviceMonitorValue;
import com.zhejiangzhengyuan.municipal_common.entity.vo.DeviceVo;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.DeviceMonitorValueService;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author :linju
 * @create on : 2019/11/25
 */
@RestController
@Slf4j
@RequestMapping("/cover/information")
@Api(tags = "井盖设备信息维护")
// 验证单个参数
@Validated
public class InformationController {

  @Autowired private DeviceService deviceService;
  @Autowired private DeviceMonitorValueService deviceMonitorValueService;

  private final String DEVICE_TYPE = ConstantUtil.DEVICE_TYPE.MANHOLE_COVER;
  private final String MODULE_TYPE = ConstantUtil.MODULE_NAME.MANHOLE_COVER;

  @ApiResponse(code = 200, message = "操作成功", response = Device.class)
  @ApiOperation("添加设备Api")
  @PostMapping("/saveDevice")
  public ResponseVo save(
      @ApiParam(name = "添加设备相关字段", required = true) @RequestBody @Validated Device device) {
    device.setType(DEVICE_TYPE);
    device.setName(DEVICE_TYPE);
    // 设备添加失败则返回服务器端错误
    return deviceService.saveDevice(device);
  }

  @ApiOperation("根据ID查找设备")
  @GetMapping("/findDeviceById/{id}")
  public ResponseVo findDeviceById(
      @ApiParam(value = "设备Id", required = true) @PathVariable Integer id) {
    Device device = deviceService.getById(id);
    return device == null
        ? ResponseVo.NOT_FOUND_RESOURCES
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, device);
  }

  @ApiOperation("根据ID更新设备")
  @PostMapping("/updateDevice")
  public ResponseVo updateDevice(@RequestBody @Validated Device device) {

    return deviceService.updateDevice(device) > 0
        ? new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, device)
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID删除设备")
  @DeleteMapping("/removeDeviceById/{id}")
  public ResponseVo removeDeviceById(@PathVariable Integer id) {
    return deviceService.removeById(id) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID 批量删除设备 格式：'ids':[7,9,10]")
  @DeleteMapping("/removeDeviceByIds")
  public ResponseVo removeDeviceByIds(@RequestBody Map<String, List<Integer>> map) {
    List<Integer> idList = map.get("ids");
    return deviceService.removeByIds(idList) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("查询设备跟设施")
  @GetMapping("/selectDeviceAndFacility")
  public Page<DeviceVo> selectDeviceAndFacility(Integer page, Integer limit) {
    page = page == null || page <= 0 ? 1 : page;
    limit = limit == null || limit <= 0 ? 15 : limit;
    Page<DeviceVo> page1 = new Page<>(page, limit);
    return deviceService.selectAllDevice(page1, DEVICE_TYPE);
  }

  @ApiOperation("井盖系统设置阈值")
  @PutMapping("/settingThreshold")
  public ResponseVo settingThreshold(@RequestBody DeviceMonitorValue deviceMonitorValue) {
    if (Objects.isNull(deviceMonitorValue.getId())) {
      deviceMonitorValue.setModuleType(MODULE_TYPE);
    }
    return deviceMonitorValueService.saveOrUpdate(deviceMonitorValue)
        ? ResponseVo.successWithData(deviceMonitorValue)
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("获取井盖系统阈值")
  @GetMapping("/getThreshold")
  public ResponseVo settingThreshold() {
    // 阈值只有一条
    QueryWrapper<DeviceMonitorValue> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("module_type", MODULE_TYPE);
    return ResponseVo.successWithData(deviceMonitorValueService.getOne(queryWrapper));
  }
}
